<?php
namespace Admin\Model;
use Think\Model;
class WorkerModel extends Model 
{
	protected $insertFields = array('code','name','sex','tel','card_code','worker_type','worker_position','worker_pri_teacher','is_show','show_sort','company_id');
	protected $updateFields = array('id','code','name','sex','tel','card_code','worker_type','worker_position','worker_pri_teacher','is_show','show_sort');
	protected $_validate = array(
		array('code', 'number', '编号必须是一个整数！', 2, 'regex', 3),
		array('name', '1,60', '姓名的值最长不能超过 60 个字符！', 2, 'length', 3),
		array('sex', '1,48', '性别的值最长不能超过 48 个字符！', 2, 'length', 3),
		array('tel', '1,48', '电话号码的值最长不能超过 48 个字符！', 2, 'length', 3),
		array('card_code', 'number', '卡号必须是一个整数！', 2, 'regex', 3),
		array('worker_type', 'number', '员工类别必须是一个整数！', 2, 'regex', 3),
		array('worker_position', 'number', '员工职位必须是一个整数！', 2, 'regex', 3),
		array('worker_pri_teacher', 'number', '是否是私教必须是一个整数！', 2, 'regex', 3),
		array('is_show', 'number', '是否显示必须是一个整数！', 2, 'regex', 3),
		array('show_sort', 'number', '显示顺序必须是一个整数！', 2, 'regex', 3),
	);
	public function search($pageSize = 20)
	{
		/**************************************** 搜索 ****************************************/
		$where = array();
		if($name = I('get.name'))
			$where['name'] = array('like', "%$name%");
		if($sex = I('get.sex'))
			$where['sex'] = array('like', "%$sex%");
		if($tel = I('get.tel'))
			$where['tel'] = array('like', "%$tel%");
		if($card_code = I('get.card_code'))
			$where['card_code'] = array('eq', $card_code);
		if($worker_type = I('get.worker_type'))
			$where['worker_type'] = array('eq', $worker_type);
        //对应瑜伽馆
        if(intval(session('admin.company_id')) !==1){
            $where['company_id'] = ['eq',session('admin.company_id')];
        }
		/************************************* 翻页 ****************************************/
		$count = $this->alias('a')->where($where)->count();
		$page = new \Think\Page($count, $pageSize);
		// 配置翻页的样式
		$page->setConfig('prev', '上一页');
		$page->setConfig('next', '下一页');
		$data['page'] = $page->show();
		/************************************** 取数据 ******************************************/
		$data['data'] = $this->alias('a')->where($where)->group('a.id')->limit($page->firstRow.','.$page->listRows)->select();
		return $data;
	}
	// 添加前
	protected function _before_insert(&$data, $option)
	{
		if(isset($_FILES['pic']) && $_FILES['pic']['error'] == 0)
		{
			$ret = uploadOne('pic', 'Worker', array(
				array(350, 350, 2),
				array(150, 150, 2),
				array(50, 50, 2),
			));
			if($ret['ok'] == 1)
			{
				$data['pic'] = $ret['images'][0];
				$data['big_pic'] = $ret['images'][1];
				$data['mid_pic'] = $ret['images'][2];
				$data['sm_pic'] = $ret['images'][3];
			}
			else 
			{
				$this->error = $ret['error'];
				return FALSE;
			}
		}
		$data['company_id'] = session('admin.company_id');
    }
	// 修改前
	protected function _before_update(&$data, $option)
	{
		if(isset($_FILES['pic']) && $_FILES['pic']['error'] == 0)
		{
			$ret = uploadOne('pic', 'Worker', array(
				array(350, 350, 2),
				array(150, 150, 2),
				array(50, 50, 2),
			));
			if($ret['ok'] == 1)
			{
				$data['pic'] = $ret['images'][0];
				$data['big_pic'] = $ret['images'][1];
				$data['mid_pic'] = $ret['images'][2];
				$data['sm_pic'] = $ret['images'][3];
			}
			else 
			{
				$this->error = $ret['error'];
				return FALSE;
			}
			deleteImage(array(
				I('post.old_pic'),
				I('post.old_big_pic'),
				I('post.old_mid_pic'),
				I('post.old_sm_pic'),
	
			));
		}
	}
	// 删除前
	protected function _before_delete($option)
	{
		if(is_array($option['where']['id']))
		{
			$this->error = '不支持批量删除';
			return FALSE;
		}
		$images = $this->field('pic,big_pic,mid_pic,sm_pic')->find($option['where']['id']);
		deleteImage($images);
	}
	/************************************ 其他方法 ********************************************/
}